package com.mp.ipm2020.mbg.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.mp.ipm2020.mbg.model.Resource;
import com.mp.ipm2020.mbg.mapper.ResourceMapper;
import com.mp.ipm2020.mbg.service.ResourceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author adidasshe
 * @since 2020-05-18
 */
@Service
public class ResourceServiceImpl extends ServiceImpl<ResourceMapper, Resource> implements ResourceService {
    @Autowired
    private ResourceMapper resourceMapper;

    @Override
    public List<Resource> list(Long categoryId, String nameKeyword, String urlKeyword, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        QueryWrapper<Resource> wrapper = new QueryWrapper<>();
        if (categoryId != null) {
            wrapper.eq("category_id", categoryId);
        }
        if (StrUtil.isNotEmpty(nameKeyword) || StrUtil.isNotEmpty(urlKeyword)) {
            wrapper
                    .like("name", nameKeyword)
                    .or()
                    .like("url", urlKeyword);
        }
        return resourceMapper.selectList(wrapper);
    }
}
